package com.psytech.app.task;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.Endpoint;

import taskmanager.psytech.com.taskmanagerservice._1.TaskManagerService_Service;

import com.psytech.app.task.ws.TaskManagerServiceImpl;

public class STMStandAlone {

	public static void publish(final String endPointURL) throws IOException {
		final URL wsdlResource = TaskManagerService_Service.class.getResource("/META-INF/TaskManagerService.wsdl");
		final List<Source> metadata = new ArrayList<Source>();

		final Source source = new StreamSource(wsdlResource.openStream());
		source.setSystemId(wsdlResource.toExternalForm());
		metadata.add(source);

		final Map<String, Object> properties = new HashMap<String, Object>();

		properties.put(Endpoint.WSDL_PORT, new QName("http://com.psytech.taskmanager/TaskManagerService/1", "TaskManagerServiceSOAP"));
		properties.put(Endpoint.WSDL_SERVICE, new QName("http://com.psytech.taskmanager/TaskManagerService/1", "TaskManagerService"));

		final Endpoint endpoint = Endpoint.create(new TaskManagerServiceImpl());
		endpoint.setMetadata(metadata);
		endpoint.setProperties(properties);

		endpoint.publish(endPointURL);
		System.out.println("WSDL => " + endPointURL + "?WSDL");// NOPMD
	}

	public static void main(final String[] args) throws Exception {
		final String endPoint = System.getProperty("task.server.endpoint");
		if (endPoint == null) {
			System.err.println("please set -Dtask.server.endpoint=http://myhost:myport/TaskService");// NOPMD
			System.exit(1);
		}
		publish(endPoint);
	}

}
